package com.example.list;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

import com.example.list.four_basic_component.NetWorkChangeReceiver;
import com.example.utils.AppLifecycler;
import com.example.utils.ToastUtil;
import com.example.utils.UtilsContext;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;

import me.weishu.reflection.Reflection;


public class MyApp extends Application {
    private IntentFilter intentFilter;
    private NetWorkChangeReceiver netWorkChangeReceiver;
    public static Context AppContext;

    @Override
    public void onCreate() {
        super.onCreate();
        //新的activity管理器
        ActivityStackManager.getInstance().init(this);
        registerLifeCycle();
        UtilsContext.init(this);
        AppContext = getApplicationContext();
        ForegroundCallbacks.init(this);




//        registerActivityLifecycleCallbacks(new AppLifecycler());
        AppLifecycler appLifecycler = new AppLifecycler();
        appLifecycler.register(this, new AppLifecycler.AppLifeCyclerCallBack() {
            @Override
            public void inBackground() {
                Log.e("capitalhoder","后台");
            }

            @Override
            public void inForeground() {
                Log.e("capitalhoder","前台");
            }
        });
        //动态注册
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        netWorkChangeReceiver = new NetWorkChangeReceiver();
        registerReceiver(netWorkChangeReceiver, intentFilter);

        Logger.addLogAdapter(new AndroidLogAdapter());//开启日志
    }

    void registerLifeCycle() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
                MyActivityManager.getInstance().setCurrentActivity(activity);
//                ToastUtil.showToast(MyActivityManager.getInstance().getCurrentActivity().getComponentName().getClassName());
            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });

    }
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        Reflection.unseal(base);
    }
}
